package com.cicadamob.contact.api.http.parse2bean;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.text.TextUtils;

import com.cicadamob.contact.api.http.bean.AssetCardDetail;
import com.gmccgz.im.sdk.http.bean.MediaUpDetail;

public class AssetXml2bean {

	public static String messageId, retcode, retmessage, createAt, content, typeName,
			typeText, media, creator_mobile, right, message, location, addrStr,
			para_int_1, title, fujian, longitude, latitude, messageMid,
			messageTime;

	public static int jsonLength;

	public static InputStream inputStream;

	public static AssetCardDetail getAssetCardDetail(String xml) {

		AssetCardDetail acd = null;
		if (!TextUtils.isEmpty(xml)) {

			acd = new AssetCardDetail();

			// 使用DocumentBuilderFactory工厂创建DocumentBuilder对象
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			try {
				// 生成DocumentBuilder
				DocumentBuilder builder = factory.newDocumentBuilder();
				// 调用完这句后XML文档解析完成，暂存在内存中
				inputStream = new ByteArrayInputStream(xml.getBytes());
				Document document = builder.parse(inputStream);
				// 获得根元素
				Element root = document.getDocumentElement();
				// 匹配结点，返回结点集
				NodeList personNodes1 = root.getElementsByTagName("header");
				Element personElement1 = (Element) personNodes1.item(0);

				// 匹配结点，返回结点集
				NodeList personNodes2 = root.getElementsByTagName("body");
				Element personElement2 = (Element) personNodes2.item(0);

				retcode = personElement2.getElementsByTagName("retcode")
						.item(0).getTextContent()
						+ "";
				
				acd.setRetcode(retcode);
				retmessage = personElement2.getElementsByTagName("retmessage")
						.item(0).getTextContent()
						+ "";
				acd.setRetmessage(retmessage);

				if ("0".equals(retcode)) {// retcode="0"成功，才继续解析

					createAt = personElement2.getElementsByTagName("createAt")
							.item(0).getTextContent()
							+ "";
					acd.setCreateAt(createAt);
					
					content = personElement2.getElementsByTagName("content")
							.item(0).getTextContent()
							+ "";
					acd.setContent(content);

					media = personElement2.getElementsByTagName("media")
							.item(0).getTextContent()
							+ "";
					acd.setMedia(media);
					message = personElement2.getElementsByTagName("message")
							.item(0).getTextContent()
							+ "";
					acd.setMessage(message);

					JSONObject jb = new JSONObject(content);
					title = jb.getString("title");
					typeText = jb.getString("text");
					fujian = jb.getString("media");

					creator_mobile = personElement2
							.getElementsByTagName("mobile").item(0)
							.getTextContent()
							+ "";
					
					acd.setMobile(creator_mobile);

					right = personElement2.getElementsByTagName("right")
							.item(0).getTextContent()
							+ "";
					
					acd.setRight(right);

					if (!TextUtils.isEmpty(fujian)) {
						JSONArray mediaArray = new JSONArray(fujian);
						for (int i = 0; i < mediaArray.length(); i++) {
							JSONObject jo = mediaArray.getJSONObject(i);
							MediaUpDetail mMediaUpDetail = new MediaUpDetail();
							mMediaUpDetail.setmDetail(jo.getString("mDetail"));
							mMediaUpDetail.setmId(jo.getString("mId"));
							mMediaUpDetail.setmName(jo.getString("mName"));
							mMediaUpDetail.setmType(Integer.parseInt((jo
									.getString("mType"))));
							// MediaUpDetailList.add(mMediaUpDetail);

						}
					}

					para_int_1 = personElement2
							.getElementsByTagName("para_int_1").item(0)
							.getTextContent()
							+ "";
					acd.setPara_int_1(para_int_1);
					

					String para_int_1_str = "";
					switch (Integer.parseInt(para_int_1)) {
					case 1:
						para_int_1_str = "未建设";
						break;
					case 2:
						para_int_1_str = "准建设";
						break;
					case 3:
						para_int_1_str = "建设中";
						break;
					case 4:
						para_int_1_str = "已投产";
						break;

					default:
						break;
					}
				}

				else {
					// do nothing
				}

			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (NullPointerException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			} finally {

				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			if (media == null || media.equals("")) {

			} else {
				try {
					JSONArray jsonList = new JSONArray(media);
					jsonLength = jsonList.length();

					// Log.v("logcat", "media=" + media + ";jsonLength=" +
					// jsonLength);
					// listhavePic.clear();

					for (int i = 0; i < jsonLength; i++) {

						JSONObject oj = jsonList.getJSONObject(i);

						String mediaDetail = oj.getString("mediaDetail");
						String sequence = "0";
						if (mediaDetail == null || "".equals(mediaDetail)) {
							if (i < 6) {
								sequence = i + "";
							} else {
								sequence = "5";
							}

						} else {
							JSONObject json = new JSONObject(mediaDetail);
							sequence = json.getString("sequence");// 序号：0，1，2，3，4
							if ("4".equals(sequence)) {
								longitude = json.getString("X");
								latitude = json.getString("Y");
							}
						}
						int j = Integer.parseInt(sequence);
						//
						// listPicOb.get(j).setCover(oj.getString("cover"));
						// listPicOb.get(j).setMedia_id(oj.getString("media_id")
						// + "");
						// listPicOb.get(j).setImg(
						// urlPicBase + oj.getString("media_id"));
						//
						// listPicOb.get(j).setHavepic(1);
						//
						// listhavePic.add(j);

					}
					// listhavePic去掉重复
					// HashSet<Integer> h = new HashSet<Integer>(listhavePic);
					// listhavePic.clear();
					// listhavePic.addAll(h);

				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			if (!TextUtils.isEmpty(message)) {
				// {"mediaId":"ff8080814b159568014b159b12810001","mediaDetail":{"time":1}}
				// Log.v("logcat", "messageMid=" + messageMid);
				// intent.putExtra("messageTime", messageTime);

				try {
					JSONObject job = new JSONObject(message);
					messageMid = job.getString("mediaId");
					JSONObject job2 = new JSONObject(
							job.getString("mediaDetail"));
					// messageTime = job2.getInt("time");
					// tv_card_voice.setText(messageTime + "”");
					// tv_card_message_time.setText(messageTime + "”");

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

		}
		return acd;

	}

	public void getContentTypeCardDetail(String xml) {

		// 使用DocumentBuilderFactory工厂创建DocumentBuilder对象
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			// 生成DocumentBuilder
			DocumentBuilder builder = factory.newDocumentBuilder();
			// 调用完这句后XML文档解析完成，暂存在内存中
			inputStream = new ByteArrayInputStream(xml.getBytes());
			Document document = builder.parse(inputStream);
			// 获得根元素
			Element root = document.getDocumentElement();
			// 匹配结点，返回结点集
			NodeList personNodes1 = root.getElementsByTagName("header");
			Element personElement1 = (Element) personNodes1.item(0);
			messageId = personElement1.getElementsByTagName("messageId")
					.item(0).getFirstChild().getNodeValue();

			// 匹配结点，返回结点集
			NodeList personNodes2 = root.getElementsByTagName("body");
			Element personElement2 = (Element) personNodes2.item(0);

			retcode = personElement2.getElementsByTagName("retcode").item(0)
					.getTextContent()
					+ "";
			retmessage = personElement2.getElementsByTagName("retmessage")
					.item(0).getTextContent()
					+ "";

			if ("0".equals(retcode)) {// retcode="0"成功，才继续解析

				createAt = personElement2.getElementsByTagName("createAt")
						.item(0).getTextContent()
						+ "";
				content = personElement2.getElementsByTagName("content")
						.item(0).getTextContent()
						+ "";

				JSONObject jb = new JSONObject(content);
				typeName = jb.getString("title");
				typeText = jb.getString("text");
				media = jb.getString("media");

				creator_mobile = personElement2.getElementsByTagName("mobile")
						.item(0).getTextContent()
						+ "";

				right = personElement2.getElementsByTagName("right").item(0)
						.getTextContent()
						+ "";

				message = personElement2.getElementsByTagName("right").item(0)
						.getTextContent()
						+ "";

				location = personElement2.getElementsByTagName("location")
						.item(0).getTextContent()
						+ "";
				String sloc = location.replace("||", "&");
				addrStr = sloc.split("&")[0];

				para_int_1 = personElement2.getElementsByTagName("para_int_1")
						.item(0).getTextContent()
						+ "";

				if (!TextUtils.isEmpty(media)) {
					JSONArray mediaArray = new JSONArray(media);
					for (int i = 0; i < mediaArray.length(); i++) {
						JSONObject jo = mediaArray.getJSONObject(i);
						MediaUpDetail mMediaUpDetail = new MediaUpDetail();
						mMediaUpDetail.setmDetail(jo.getString("mDetail"));
						mMediaUpDetail.setmId(jo.getString("mId"));
						mMediaUpDetail.setmName(jo.getString("mName"));
						mMediaUpDetail.setmType(Integer.parseInt((jo
								.getString("mType"))));

					}
				}

				if (TextUtils.isEmpty(right)) {

				} else {
					JSONObject joRight = new JSONObject(right);

					JSONArray jaAdmin = joRight.getJSONArray("admin");
					JSONArray jaReading = joRight.getJSONArray("reading");
					JSONArray jaWriting = joRight.getJSONArray("writing");

				}

			} else {
				// do nothing
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {

			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		if (media == null || media.equals("")) {

		} else {

		}

	}

}
